﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Data.Linq;
using System.Windows.Forms;
using QLTVNEW.Properties;
using DevComponents.DotNetBar;
namespace QLTVNEW
{
    public partial class frmTacGia : DevComponents.DotNetBar.Office2007Form
    {
        public frmTacGia()
        {
            InitializeComponent();

        }

        #region 'Thành Phần'
        void txtReadOnly(bool a)
        {
            txtMa.ReadOnly=a;
            txtTen.ReadOnly=a;
            txtDC.ReadOnly=a;
  
  
        }

        void btnThem(bool a)
        {
            btnupdate.Enabled=a;
            btndel.Enabled=a;
        }

        void btnSua(bool a)
        {
            btninsert.Enabled=a;
            btndel.Enabled=a;
        }
        #endregion

        QLTVDataContext db = new QLTVDataContext();

        private void frmTacGia_Load(object sender, EventArgs e)
        {
            txtReadOnly(true);
            var query=from tg in db.TACGIAs select tg;
            GVW.DataSource = query;
            txtMa.Text = GVW.Rows[0].Cells[0].Value.ToString().Trim();
            txtTen.Text = GVW.Rows[0].Cells[1].Value.ToString().Trim();
            txtDC.Text = GVW.Rows[0].Cells[2].Value.ToString().Trim();

        }

        private void frmTacGia_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.AutoValidate = AutoValidate.Disable;
        }

        private void btnclear_MouseMove(object sender, MouseEventArgs e)
        {
          
        }

        private void btnclear_MouseHover(object sender, EventArgs e)
        {
          

        }

        private void btnclear_Click(object sender, EventArgs e)
        {
            txtReadOnly(false);
            foreach (Control ctr in groupBox1.Controls)
            {
                if (ctr is TextBox)
                {
                    ctr.Text="";
                    labelX1.Focus();
                }
            }
        }

        private void btninsert_Click(object sender, EventArgs e)
        {
            txtMa.ReadOnly = true;
            btnsave.Enabled = true;
            btninsert.Image = Resources.Cancel;
            btnThem(false);
            txtReadOnly(false);
            superTooltip1.SetSuperTooltip(btncancel, new SuperTooltipInfo("Hủy Bỏ.", "", "", null, null, eTooltipColor.Lemon));
        }

        private void btnupdate_Click(object sender, EventArgs e)
        {
            superTooltip1.SetSuperTooltip(btncancel, new SuperTooltipInfo("Hủy Bỏ.", "", "", null, null, eTooltipColor.Lemon));
            btnsave.Enabled = true;
            btnSua(false);
            btnupdate.Image = Resources.Cancel;
            txtReadOnly(false);
            txtMa.ReadOnly = true;
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            if (btnsave.Enabled == true)
            {
                txtReadOnly(true);
                btnSua(true);
                btnThem(true);
                btninsert.Image = Resources.add1_;
                btnupdate.Image = Resources.edit;
                btnsave.Enabled = false;
              
            }
            else
            {
                
                this.AutoValidate = AutoValidate.Disable;
                this.Close();
            }
        }

        private void btnsave_Click(object sender, EventArgs e)
        {
            if (btninsert.Enabled == false)
            {
                if (txtMa.Text != "")
                {
                    TACGIA tg = db.TACGIAs.Where(t => t.MaTacGia == int.Parse(txtMa.Text)).SingleOrDefault<TACGIA>();
                    if (tg != null)
                    {
                        db.Connection.Open();
                        db.Transaction = db.Connection.BeginTransaction();
                        try
                        {
                            tg.TenTacGia = txtTen.Text.Trim();
                            tg.GhiChu = txtDC.Text.Trim();
                            db.SubmitChanges();
                            db.Transaction.Commit();
                            MessageBox.Show("Sửa Thành Công");
                            var query = from tgs in db.TACGIAs select tgs;
                            GVW.DataSource = query;
                        }
                        catch (Exception ex)
                        {
                            db.Transaction.Rollback();
                            MessageBox.Show(ex.Message);
                        }
                        finally
                        {
                            if (db.Connection.State == ConnectionState.Open)
                            {
                                db.Connection.Close();
                            }
                            db.Transaction = null;
                            btnupdate.Image = Resources.edit;
                            btnSua(true);
                            txtReadOnly(true);
                            btnsave.Enabled = false;
                        }
                    }

                }
            }
           if (btnupdate.Enabled==false)
           {
               string err = "";
               db.uspCRUDTACGIA('t', 0, txtTen.Text, txtDC.Text, ref err);
               MessageBox.Show(err.ToString());
               db.SubmitChanges();
               var query = from tg in db.TACGIAs select tg;
               GVW.DataSource = query;
               txtReadOnly(true);
               btninsert.Image = Resources.add1_;
               btnThem(true);
               btnsave.Enabled = false;
           }
        }
        int dong;
        private void GVW_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                dong=e.RowIndex;
                txtMa.Text=GVW.Rows[dong].Cells[0].Value.ToString().Trim();
                txtTen.Text=GVW.Rows[dong].Cells[1].Value.ToString().Trim();
                txtDC.Text=GVW.Rows[dong].Cells[2].Value.ToString().Trim();
            }
            catch
            {
                return;
            }
        }

        private void btndel_Click(object sender, EventArgs e)
        {
            if (txtMa.Text != "")
            {
                string err = "";
                if (MessageBox.Show("Bạn Có Muốn Xóa Không", "Thông Báo",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
                {
                    db.uspCRUDTACGIA('x', int.Parse(txtMa.Text), txtTen.Text,txtDC.Text, ref err);
                    MessageBox.Show(err.ToString());
                    db.SubmitChanges();
                    var query = from tg in db.TACGIAs select tg;
                    GVW.DataSource = query;
                }
             
            }
            else
            {
                MessageBox.Show("Chưa nhập Mã Tác Giả");
            }

        }
    }
}
